package com.javaee.xiangyang.controller.mall;

import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.entity.Comment;
import com.javaee.xiangyang.entity.User;
import com.javaee.xiangyang.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/MallComment")
public class MallCommentController {

    @Resource
    CommentService commentService;

    /**
     * 用户发表评论
     */
    @RequestMapping("/AddComment")
    public ModelAndView addComment(ModelAndView modelAndView, Comment comment, HttpSession session) {
        User user = (User) session.getAttribute("user");
        comment.setCommentUserAccount(user.getUserAccount());
        ServiceResultEnum serviceResultEnum = commentService.addComment(comment);
        if (serviceResultEnum.equals(ServiceResultEnum.SUCCESS)) {
            modelAndView.setViewName("redirect:/MallGoods/GoodsDetail?goodsId=" + comment.getCommentGoodsId());
            return modelAndView;
        }
        modelAndView.addObject("error", serviceResultEnum.getResult());
        modelAndView.setViewName("/error/error");
        return modelAndView;
    }

    /**
     * 前往查看评论界面
     */
    @RequestMapping("/ToComment")
    public ModelAndView getAllCommentByUserAccount(ModelAndView modelAndView,HttpSession session) {
        User user = (User) session.getAttribute("user");
        modelAndView.addObject("comments", commentService.findAllCommentByUserAccount(user));
        modelAndView.setViewName("/mall/customer-pinglun");
        return modelAndView;
    }

    /**
     * 删除一条评论
     */
    @RequestMapping("/DeleteComment")
    public ModelAndView deleteComment(ModelAndView modelAndView, Comment comment, HttpSession session) {
        User user = (User) session.getAttribute("user");
        comment.setCommentUserAccount(user.getUserAccount());
        ServiceResultEnum serviceResultEnum = commentService.deleteCommentByCommentId(comment);
        if (serviceResultEnum.equals(ServiceResultEnum.SUCCESS)) {
            modelAndView.setViewName("redirect:/MallComment/ToComment");
            return modelAndView;
        }
        modelAndView.addObject("error", serviceResultEnum.getResult());
        modelAndView.setViewName("/error/error");
        return modelAndView;
    }


}
